/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfaceRecebeCreci.java
 *
 * Created on 06/07/2010, 19:30:00
 */

package imobiliaria.gui;

import imobiliaria.controladores.ControladorFuncionario;
import imobiliaria.controladores.ControladorPedidos;
import imobiliaria.entidades.Funcionario;
import imobiliaria.entidades.Pedido;
import imobiliaria.entidades.Sistema;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author thiagofp
 */
public class InterfaceRecebeCreci extends javax.swing.JFrame {
    private Pedido pedido;
    private Sistema sis;
    private InterfaceOpIF tela;
    /** Creates new form InterfaceRecebeCreci */
    public InterfaceRecebeCreci(Pedido pedido, InterfaceOpIF tela) {
        sis = new Sistema();
        this.tela = tela;
	try {
	    sis.atualizaDados();
	} catch (Exception e) {
	    System.out.println("Erro: " + e.getMessage());
	}
        this.pedido = pedido;
        setVisible(true);
        initComponents();
        setLocationRelativeTo(null);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        JTF_Creci = new javax.swing.JTextField();
        JB_OK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Digite o CRECI do funcionário:");

        JB_OK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/okIcon.png"))); // NOI18N
        JB_OK.setText("OK");
        JB_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_OKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(JTF_Creci, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(JB_OK)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addComponent(JTF_Creci, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(JB_OK)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JB_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_OKActionPerformed
        retornaNome();
    }//GEN-LAST:event_JB_OKActionPerformed

    public void retornaNome(){
        String creci = JTF_Creci.getText();
        Funcionario func;
        try {
        func = ControladorFuncionario.getInstance().getFuncionarioPorCreci(creci.trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Funcionário Inválido",
                    "CRECI", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            ControladorPedidos.getInstance().efetuaPedido(pedido.getImovel().getRegistroImovel() + "", func.getCreci());

            JOptionPane.showMessageDialog(null, "Pedido efetuado com sucesso!",
                    "Pedido", JOptionPane.INFORMATION_MESSAGE);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao efetuar pedido:\n\n" + ex.getMessage(),
                    "Erro", JOptionPane.ERROR_MESSAGE);
        }

        sis.salvarDados();
        tela.atualiza();
        dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfaceRecebeCreci(null,null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_OK;
    private javax.swing.JTextField JTF_Creci;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
